package whatHandlers;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;

public class whatCDLogin {
	
	//
	
	private final static String SSLLogin = "https://ssl.what.cd/login.php";
	private final static String NormalLogin = "http://what.cd/login.php";
	
	private final WebParserRegex WPR;
	
	private boolean LOGGEDIN = false;
	
	private CookieStore CookieStorage;
	private final String LoginPageStr;

	private final HttpClient CLIENT;
	private final HttpContext LOCALCONTEXT;
	private final HttpPost LOGINPAGE;
	private HttpResponse LOGINRESPONSE;
	private final ResponseHandler<String> RESPHAND;
	private final List<NameValuePair> NVP;
	
	//Variables initialized after execution of any methods.
	
	private String USERPAGE;

	public whatCDLogin(String Username, String Password) {
		WPR = new WebParserRegex();
		
		LoginPageStr = SSLLogin;

		CLIENT = new DefaultHttpClient();
		LOCALCONTEXT = new BasicHttpContext();
		LOGINPAGE = new HttpPost(LoginPageStr);
		RESPHAND = new BasicResponseHandler();

		NVP = new ArrayList<NameValuePair>();
		NVP.add(new BasicNameValuePair("username", Username));
		NVP.add(new BasicNameValuePair("password", Password));
		NVP.add(new BasicNameValuePair("keeplogged", "1"));
		LOCALCONTEXT.setAttribute(ClientContext.COOKIE_STORE, CookieStorage);
	}

	public whatCDLogin(String[] UsernamePassword) {
		WPR = new WebParserRegex();
		
		LoginPageStr = SSLLogin;

		CLIENT = new DefaultHttpClient();
		LOCALCONTEXT = new BasicHttpContext();
		LOGINPAGE = new HttpPost(LoginPageStr);
		RESPHAND = new BasicResponseHandler();

		NVP = new ArrayList<NameValuePair>();
		NVP
				.add(new BasicNameValuePair("username",
						UsernamePassword[0]));
		NVP
				.add(new BasicNameValuePair("password",
						UsernamePassword[1]));
		NVP.add(new BasicNameValuePair("keeplogged", "1"));
		LOCALCONTEXT.setAttribute(ClientContext.COOKIE_STORE, CookieStorage);
	}

	public boolean loginPOST() {
		String loginResponse = new String();
		HttpGet LoginPageRespGET = new HttpGet(LoginPageStr);

		try {
			LOGINPAGE.setEntity(new UrlEncodedFormEntity(NVP, HTTP.UTF_8));
		} catch (UnsupportedEncodingException e) {
			return false;
		}

		try {
			LOGINRESPONSE = CLIENT.execute(LOGINPAGE, LOCALCONTEXT);
			LOGINRESPONSE.getEntity().consumeContent();
		} catch (ClientProtocolException e) {
			return false;
		} catch (IOException e) {
			return false;
		}

		try {
			loginResponse = CLIENT.execute(LoginPageRespGET, RESPHAND,
					LOCALCONTEXT);
		} catch (ClientProtocolException e) {
			return false;
		} catch (IOException e) {
			return false;
		}

		if (loginResponse.contains("loginform")) {
			return false;
		} else {
			USERPAGE = WPR.getUserIDLink(loginResponse);
			LOGGEDIN = true;
			System.out.println();
			return true;
		}
	}

	public String getPage(String Page) {
		if (!LOGGEDIN) {
			return null;
		} else {
			String PageData = new String();
			HttpGet PAGETOGET = new HttpGet(Page);
			try {
				PageData = CLIENT.execute(PAGETOGET, RESPHAND, LOCALCONTEXT);
			} catch (ClientProtocolException e) {
				return null;
			} catch (IOException e) {
				return null;
			}
			return PageData;
		}

	}

}// ends whatCDLogin
